package com.entities.user.requestmapparser;

import com.core.utils.RequestParams;
import com.entities.usergroup.servicelayer.UserGroupServiceLayer;
import com.entities.userrole.servicelayer.UserRoleServiceLayer;
import com.model.user.User;
import com.model.user.UserGroup;
import com.model.user.UserRole;


public class UserMapParser {

	public static User fromMap ( RequestParams requestParams ) {

		if ( requestParams == null ) {
			return null;
		}


		Integer userRoleId = requestParams.integerAtIndexForKey(0, "userRole");

		Integer userGroupId = requestParams.integerAtIndexForKey(0, "userGroup");

		String name = requestParams.stringAtIndexForKey(0, "name");

		String description = requestParams.stringAtIndexForKey(0, "description");

		String mailAddress = requestParams.stringAtIndexForKey(0, "mailAddress");

		UserRoleServiceLayer userRoleServiceLayer = new UserRoleServiceLayer();
		UserRole userRole = (UserRole) userRoleServiceLayer.objectByUNID(userRoleId, null, null);

		UserGroupServiceLayer userGroupServiceLayer = new UserGroupServiceLayer();
		UserGroup userGroup = (UserGroup) userGroupServiceLayer.objectByUNID(userGroupId, null, null);


		User template = null;
		template = new User();
		template.setName(name);
		template.setDescription(description);
		template.setMailAddress(mailAddress);
		template.getUserCategory().setUserRole(userRole);
		template.getUserCategory().setUserGroup(userGroup);

		return template;
	}
}
